package com.iwhalecloud.bss.kite.cucc.service.attr.handler;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;

/**
 * SDH-区县
 */
public class LocalZcCountryAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        //重提初始化不校验
        //fixme 可能导致频繁访问缓存,上线临时处理方案
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (Objects.nonNull(extParams)&& extParams.containsKey("nodeCode")) {
            return;
        }

        // 变更业务的场景初始化过程中不校验
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(
            querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))
            || KiteStringUtils.equals(attr.getOldValue(), value)) {
            return;
        }

        String eRentScopeAttrValue = inst.getAttrValue("2_0000004");
        String otherFieldName = KiteStringUtils.equals(attr.getFieldName(), "2_0000222")?"2_0000111":"2_0000222";
        String otherValue = inst.getAttrValue(otherFieldName);
        if(KiteStringUtils.isAnyEmpty(eRentScopeAttrValue,value,otherValue)){
            return;
        }
    }

}
